//
//  marqueeView.m
//  testTableVIew
//
//  Created by dukai on 16/10/8.
//  Copyright © 2016年 DR. All rights reserved.
//

#import "marqueeView.h"
#import "NSTimer+BlocksKit.h"

#define marqueeView_cellHeight self.frame.size.height
@implementation marqueeView
{
    NSTimer *timer_start;
    NSMutableArray *allArr;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
    
    }
    return self;
}

- (void)scrollViewIndex:(marqueeViewBlock)block{
    
    if (block) {
        self.blockScrollIndex =block;
    }
}

- (void)start:(marqueeViewBlock)block{
    
    if (block) {
        self.block=block;
    }
    
    self.count = 0;
    
//    self.tableView = [[UITableView alloc]initWithFrame:self.bounds];
//    self.tableView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
//    self.tableView.delegate = self;
//    self.tableView.dataSource = self;
//    self.tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
//    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
//    self.tableView.userInteractionEnabled = YES;
//    [self addSubview:self.tableView];
//    self.tableView.showsVerticalScrollIndicator = NO;
//    self.tableView.scrollEnabled = NO;
    [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    allArr = [[NSMutableArray alloc]init];
    [allArr addObjectsFromArray:self.dataArray];
    [allArr insertObject:self.dataArray[0] atIndex:allArr.count];
    [self.tableView reloadData];
    _interval = _interval>0?_interval:3;
    if (timer_start == nil) {
    timer_start = [NSTimer bk_scheduledTimerWithTimeInterval:_interval block:^(NSTimer *timer) {
            [self tick];
        } repeats:YES];
    }

}

- (void) tick {
    self.count ++;
    [self.tableView setContentOffset:CGPointMake(0, marqueeView_cellHeight * (float)self.count) animated:YES];
    if (self.count == allArr.count-1) {
        self.count = 0;
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.3 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
             [self.tableView setContentOffset:CGPointMake(0, marqueeView_cellHeight * (float)self.count) animated:NO];
        });
    }
    if (self.blockScrollIndex) {
        self.blockScrollIndex(self.count);
    }
}

-(void)selectIndex{
    if (self.block) {
        self.block(self.count);
    }
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return allArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"cell"];
        _fontSize =  _fontSize>0?_fontSize:aufont(10.5);
        cell.textLabel.font = [UIFont systemFontOfSize:_fontSize];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        if (self.textColor) {
            cell.textLabel.textColor = self.textColor;
        };
        cell.textLabel.textAlignment = NSTextAlignmentLeft;
    }
    cell.textLabel.text = allArr[indexPath.row];
    return cell;
}

- (CGFloat) tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    return marqueeView_cellHeight;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    NSLog(@"热点");
    self.block(self.count);
}

- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:self.bounds];
        _tableView.backgroundColor = [[UIColor whiteColor]colorWithAlphaComponent:0];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle  = UITableViewCellSeparatorStyleNone;
        _tableView.userInteractionEnabled = YES;
        [self addSubview:_tableView];
        _tableView.showsVerticalScrollIndicator = NO;
        _tableView.scrollEnabled = NO;
    }
    return _tableView;
}

@end
